﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic.CmsContents;
using Module.Core.Common;
using Module.Core.EntitiesInfo.TablesInfo;
using Module.Core.BusinessLogic.NewLetterEmails;
using System.Text;
using Module.Core.LoginSystem.Commons.Framwork;

namespace Web.Cms.NewsLetter_Emails
{
    public partial class NewsLetter_Emails : AdminPage
    {
        #region Khai báo
        private int EmailId = 0;
        
        #endregion
        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                EmailId = ObjConvertUtility.ToInt32(Request.QueryString["EmailId"]);
                if (!Page.IsPostBack)
                {
                    LoadData();
                    txtCreateOn.SelectedDate = DateTime.Now;

                }
            }
            catch (Exception ex)
            {
            }
        }
        #endregion

         #region Thêm mới
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                NewsLetterEmails info = new NewsLetterEmails();
                FillData(info);
                int i = NewLetterEmailsBL.Insert(info);
                if (i > 0)
                {
                    lblUpdateStatus.Text = "Bạn đã thêm mới thành công";
                    ClearText();
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region Load Data DropList

        void DropGroupId()
        {
            //CMSGroupsInfo groupInfo = new CMSGroupsInfo();

            //DataTable tb = CMSGroupsBL.GetGroupName(groupInfo.GroupID, groupInfo.Title);
            //dropGroupId.DataSource = tb;

            //dropGroupId.DataTextField = "Title";
            //dropGroupId.DataValueField = "GroupID";
            //dropGroupId.DataBind();
            //dropGroupId.Items.Insert(0, new ListItem("Lựa chọn nhóm", "0"));
        }




        #endregion

        #region FillData
        protected void FillData(NewsLetterEmails info)
        {
            info.FullName = txtName.Text;      
            info.Date = DateTime.Now;
            info.Address = txtdiachi.Text;            
            info.Email = txtEmail.Text;
            info.EmailId = EmailId;
            info.GroupId = Convert.ToInt32(dropGroupId.SelectedValue.ToString());
        }
        #endregion

        #region ClearText
        void ClearText()
        {
        
            txtEmail.Text = "";
            //txtCreatedBy.Text = "";
            txtdiachi.Text = "";
            txtName.Text = "";

      
        }
        #endregion

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                NewsLetterEmails info = new NewsLetterEmails();
                FillData(info);
                NewLetterEmailsBL.Update(info);
                lblUpdateStatus.Text = "Bạn đã chỉnh sửa thành công";
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }

        #region LoadData
        private void LoadData()
        {
            try
            {
                if (EmailId > 0)
                {
                    btnUpdate.Visible = true;
                    btnAdd.Visible = false;
                    //NewsLetterEmails info = new NewsLetterEmails();
                    NewsLetterEmails info = NewLetterEmailsBL.Select(EmailId.ToString(),1);
                    if (info != null)
                    {
                        txtName.Text = info.FullName;
                        txtEmail.Text = info.Email;
                        txtdiachi.Text = info.Address;
                        dropGroupId.SelectedValue = info.GroupId.ToString();

                    }
                }
                else
                {
                    btnUpdate.Visible = false;
                    btnAdd.Visible = true;
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion
    }
}